//
//  FourViewController.m
//  豆瓣
//
//  Created by hy on 15/12/31.
//  Copyright © 2015年 hongyu. All rights reserved.
//

#import "FourViewController.h"
#import "zhuceViewController.h"
#import "SDImageCache.h"
#import "activityViewController.h"
#import "MovieViewController.h"
@interface FourViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIBarButtonItem *item;
@end

@implementation FourViewController

- (void)dealloc {
    [_item release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.navigationItem.title = @"个人中心";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.item = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = _item;
    [_item release];
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    table.rowHeight = 50;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.separatorColor = [UIColor blackColor];
    [self.view addSubview:table];
    [table release];
    table.delegate = self;
    table.dataSource = self;
    [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }
    else if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"清除缓存";
    }
    cell.textLabel.textColor = [UIColor purpleColor];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
//        sdimage 清除缓存方法
        [[SDImageCache sharedImageCache] clearDisk];
//        计算缓存
//        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
//        CGFloat newSize = size / 1024.0 / 1024.0;
//        NSLog(@"%.2fMB", newSize);
//        NSString *str = [NSString stringWithFormat:@"清除%.2fM缓存", newSize];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"清除成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{
        }];
    } else if (indexPath.row == 0) {
        [self activityAction];
    }
    else {
        [self movieAction];
    }
}
- (void)itemAction:(UIBarButtonItem *)item {
   
    if ([_item.title  isEqual: @"注销"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注销成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _item.title = @"登录";
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{
        }];
    }

    zhuceViewController *vc = [[zhuceViewController alloc] initWithNibName:@"zhuceViewController" bundle:[NSBundle mainBundle]];
    vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    vc.block = ^(NSString *str) {
        _item.title = str;
    };
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:^{
    }];
}

//我的活动收藏页
- (void)activityAction {
    activityViewController *vc = [[activityViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
//我的电影收藏页
- (void)movieAction {
    MovieViewController *vc = [[MovieViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
